package me.zxk.qwert_poetry.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import me.zxk.qwert_poetry.dao.Tag;
import me.zxk.qwert_poetry.dto.PageResult;
import me.zxk.qwert_poetry.dto.request.TagIdDTO;
import me.zxk.qwert_poetry.dto.response.Response;
import me.zxk.qwert_poetry.service.TagService;
import me.zxk.qwert_poetry.util.VerifyToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@io.swagger.v3.oas.annotations.tags.Tag(name = "标签接口")
@RestController
public class TagController {
    @Autowired
    private TagService tagService;

    @Operation(summary = "列出所有标签")
    @GetMapping("/api/tags/list")
    public Response<PageResult<Tag>> listTags(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        return Response.ok(new PageResult<Tag>(tagService.listTags(page, size)));
    }

    @Operation(summary = "添加标签", description = "ID字段会被忽略")
    @PostMapping("/api/tags/add")
    @VerifyToken(needAdmin = true)
    public Response<Tag> addTag(@RequestBody Tag tag) {
        return Response.ok(tagService.addTag(tag));
    }

    @Operation(summary = "编辑标签", description = "所有的字段都会被覆盖")
    @PostMapping("/api/tags/edit")
    @VerifyToken(needAdmin = true)
    public Response<Tag> editTag(@RequestBody Tag tag) {
        return Response.ok(tagService.editTag(tag));
    }

    @Operation(summary = "删除标签")
    @PostMapping("/api/tags/delete")
    @VerifyToken(needAdmin = true)
    public Response<Void> deleteTag(@RequestBody TagIdDTO dto) {
        tagService.deleteTag(dto.getId());
        return Response.ok();
    }

    @Operation(summary = "根据ID或名称获取标签")
    @GetMapping("/api/tags/get")
    public Response<Tag> getTag(
            @RequestParam(required = false) Integer id,
            @RequestParam(required = false) String name
    ) {
        if (id != null)
            return Response.ok(tagService.getTagById(id));
        else if (name != null)
            return Response.ok(tagService.getTagByName(name));
        else
            return Response.error("参数错误", 1);
    }
}
